/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.DRHclient.servlet;

import fr.DRHclient.utils.AuthManager;
import fr.annuaireComp.clientUtils.ProfileModel;
import fr.annuaireComp.sessions.PersonnelManager;
import fr.annuaireComp.entities.Personnel;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author MiniK
 */
@WebServlet(name = "addnew", urlPatterns = {"/addnew"})
public class addnew extends HttpServlet {

    @EJB
    PersonnelManager persMan;

  
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (!AuthManager.logUser(request, response)) {
            return;
        }
        request.getRequestDispatcher("/WEB-INF/addnew.jsp").forward(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (!AuthManager.logUser(request, response)) {
            return;
        }

        String nom = "";
        String prenom = "";
        try {
            nom = request.getParameter("nom").trim();
            prenom = request.getParameter("prenom").trim();
            Date dateNaiss = new SimpleDateFormat("dd/MM/yyyy").parse(request.getParameter("datenaissance"));
            boolean sexe = false;
            if (request.getParameter("sexe").equals("F")) {
                sexe = true;
            }

            String bureau = request.getParameter("bureau");

            Integer idFonction = Integer.parseInt(request.getParameter("fonction"));



            Personnel p = persMan.addPersonnel(nom, prenom, dateNaiss, bureau, sexe, idFonction);
            ProfileModel profileData = ProfileModel.buildProfileModel(p);
            request.setAttribute("profiledata", profileData);
            request.setAttribute("profiletemppass", p.getPassword());

            request.getRequestDispatcher("/WEB-INF/addnewsuccess.jsp").forward(request, response);
        } catch (Exception ex) {
            if (nom.isEmpty() || prenom.isEmpty()) {
                request.setAttribute("errormsg", "Veillez entrer un nom ou un prenom");
            } else {
                request.setAttribute("errormsg", "Une erreur est servenue lors de la création du personnel");
            }
            request.getRequestDispatcher("/WEB-INF/error.jsp").forward(request, response);
        }

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
